/**
 * @name 联合早报
 * @appid 1916823
 */

import Creeper, { _, extractFields } from '../lib/creeper';

const app = new Creeper({
  domains: ['www.zaobao.com'],
  scanUrls: [
    'http://www.zaobao.com/',
    'http://www.zaobao.com/wencui/politic',
    'http://www.zaobao.com/wencui/social',
    'http://www.zaobao.com/finance/china',
    'http://www.zaobao.com/news/china',
    'http://www.zaobao.com/forum/views',
    'http://www.zaobao.com/forum/editorial',
    'http://www.zaobao.com/forum/comic',
  ],
  contentUrlRegexes: [/http:\/\/www\.zaobao\.com\/(\w+\/)+story20\d+-\d+/], // 内容页url正则
  fields: [
    {
      name: 'time',
      alias: '时间',
      selector: '//meta[contains(@property,"article:published_time")]/@content',
      required: true,
    },
    {
      name: 'image',
      alias: '配图',
      selector: '//picture/source[1]/@data-srcset',
    },
    {
      name: 'content',
      alias: '内容',
      selector: '//div[@id="FineDining"]',
    },
  ]
});

app.onProcessScanPage((page, content, site) => {
  let str = content;
  let re;

  if (page.url === 'http://www.zaobao.com/') {
    // 首页处理
    content.replace(/<section id="piping-hot">[\s\S]+?<\/section>/, (m) => {
      str = m;
      return m;
    });
    re = /<li class="post-item(?:[^<]+<){2}a href="([^"]+)(?:[^>]+>){3}([^<]+)(?:[^>]+>){2}([^<]+)/g;
  } else {
    // 频道页处理
    re = /span><a href="([^"]+)"(?:[^>]+>){3}([^<]+)(?:[^>]+>){5}([^<]+)<\/span/g;
  }

  str.replace(re, (m, url, title) => {
    site.addUrl(url); // 添加内容列表
    _.store.set(url, {
      title: title.trim(),
    });
    return m;
  });

  return false; // 关闭自动url发现
});

app.afterExtractField((fieldName, data, page, site, index) => {
  extractFields(fieldName, data, page, site, index);

  if (page.url.indexOf('/forum/comic') > 0) {
    if (fieldName === 'image') {
      const img = data.replace(/\s+\dx/,'');
      _.store.set(page.url + 'img', img);
      return hostFile(img, FileType.IMAGE);
    }
    if (fieldName === 'content') {
      return '<img src="' + _.store.get(page.url + 'img') + '" title="' + _.store.get(page.url).title + '">';
    }
  }

  if (fieldName === 'image' && data) {
    return hostFile(data.replace(/\s+\dx/, ''), FileType.IMAGE);
  }

  if (fieldName === 'time') {
    const dt = new Date(data);
    if (String(dt) === 'Invalid Date' || _.expired(dt, '24h')) {
      page.skip();
    }
    return String(+dt).slice(0, 10);
  }

  if (fieldName === 'lead' && !data) {
    return _.store.get(page.url).title;
  }

  return data;
});

app.start();
